package com.xs.gis.domain.geojson;

import com.xs.gis.util.JacksonUtil;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.geotools.geojson.geom.GeometryJSON;
import org.locationtech.jts.geom.Geometry;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Classname GeoGeometry
 * @Description 一个数据
 * @Date 2024/9/26 14:38
 * @Created w
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public  class GeoFeature {
    private  String type;
    private Map<String,Object> properties=new HashMap<>();

    private BaseGeometry geometry;

    private List<BaseGeometry>  geometries;

    /***
     * 格式化为Geometry
     * @param srid
     * @return
     */
    public Geometry formatGeometry(int srid) {
        Reader reader = new StringReader(JacksonUtil.toJsonString(geometry));

        GeometryJSON gjsonNew = new GeometryJSON();
        Geometry newGeometry = null;
        try {
            if (Objects.equals(String.valueOf(geometry.getType()),Geometry.TYPENAME_GEOMETRYCOLLECTION)){
                newGeometry = gjsonNew.readGeometryCollection(reader);
            }else {
                newGeometry = gjsonNew.read(reader);
                newGeometry.setSRID(srid);
            }
            return newGeometry;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    /**
     * 默认格式化为 4490
     *
     * @param
     * @return
     */
    public Geometry formatGeometry() {
        return formatGeometry(4490);
    }

    /***
     * 属性转为具体对象
     * @param clazz
     * @return
     * @param <T>
     */

    public <T> T propertiesToBean(Class<T> clazz) {
        return JacksonUtil.toObject(JacksonUtil.toJsonString(properties),clazz);
    }
}
